package com.example.prom.ok.y2023.work.recaptcha;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.prom.ok.y2022.dto.taxbit.GetTaxBitUserRespDTO;
import com.example.prom.ok.y2022.enums.ResponseErrorCodeEnum;
import com.example.prom.utils.HttpClientSingleton;
import com.example.prom.utils.HttpNewUtils;
import com.example.prom.utils.ResponseResult;
import com.google.protobuf.ByteString;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.example.prom.ok.y2022.enums.ResponseErrorCodeEnum.SUCCESS;

@Controller
@RequestMapping("recaptcha")
public class RecaptchaController {

    @Autowired
    private CreateAssessment createAssessment;

    @Autowired
    private RestTemplate restTemplate;


    @ResponseBody
    @RequestMapping("test")
    public void test(String token, String action) throws IOException {
        createAssessment.createAssessment("lanzhizhuxia", "6Lee36MkAAAAAO5exxBKI2TJQ9rMpwikPtVCQZ91", token, action);
    }

    @RequestMapping("test2")
    public String test2() {
        return "recaptchaPageForm";
    }

//    public static void main(String[] args) throws Exception {
//        String apiKey = "AIzaSyASGLW2py0nsgOySX3bKmw7-ZTafmtfw7A";
//        String requestUrl = "https://recaptchaenterprise.googleapis.com/v1/projects/lanzhizhuxia/assessments";
//        HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();
////        Map<String, String> headerMap = new HashMap<>();
////        headerMap.put("authorization",token);
//        Map<String, String> querys = new HashMap<>();
//        querys.put("key", apiKey);
//        RecaptchaReqDTO recaptchaReqDTO = new RecaptchaReqDTO();
//        RecaptchaEvent recaptchaEvent = new RecaptchaEvent();
//        recaptchaEvent.setSiteKey("6Lee36MkAAAAAO5exxBKI2TJQ9rMpwikPtVCQZ91");
//        recaptchaEvent.setToken("03AFY_a8WuYDyi8nW1K8lE8GRIppVP4cciqP_diEqcXcdcP8Z3Sq--3SvQxO1OSviusrvl9LVyhDiCCd6i1WXCRX5vEqd98EI9JmGdhphkLdhJVf0Uq8reBOonyyOhdPsGN7kn91TvcerL1OCzWdMyJ5cYRRIIO9WgSmMIqfGKntUuNLchV04p2-WmLkQJJFxawnn6wka11Pw5VDci0cSlSDeBQgQGZPFBynxyLxTaGe7cM0_JFupJu19l3p2v7ZJrCBZAl1cHVrego0uyTgCZQm5m23bL1DdcHNlpthJHu_bVS7TVN3i8azZg1N6esl8joyONXQ-VwEfCWLa5beU5tC_eaD09Gf5qrnWfW80JY50QT0BNw0KSIH-lI00qdTLNgwWrnntt9Ff7hwwd0yKmO9WLdlGeOOuFUNQxtjjb4A2OOkz3VVVsMZpG0dktMlOjDagNPUWz3VBIukYt0Vu-Qcf3Otm_R1CHzI3cgrGh7HTK4lbrR6sB80LMn7fe8MSncxCw12xtZPhd9H_-xFTKK2w5BTOJur6xXWMwPdipwhn3MXJse1HcpgsaFHUKjTZx7D4OecY3XRO3g_Y-A62JtBEH5BV5odbH5JtA86mkJ1eHUSMopBV-UJyDGaVSc8RUK8VpKPS37e0XKJaNaHtAvFKhxzuc-k767oIYDbzdhYdaxHM1_-YkKhybsBBa74xQjNi9j7CgHHbDxx_kGWkCUpny3pciJRZdG7yCcvvxQ_K23ox2uG-VdJpssZBQZoiAOefX_MiWKyUBB86HvX8Pbgm5d_hGenydl45JPGPMobzb6ThHS1afmP2q1OM_zrGABtk9H1G_hwF9h2w4fqHFn-eB6B3FeHllwcw0ox_oFDkD_k4RboCPminpRiZbSEr563C2HKDbpLkHXdP8CLnED6RGkfQWrqsIuPVJ0TpspOl1cSAfd29h1qfT1XUxPNx7HooOTMox3aoPW60Ka8l2xX4km-YRbtrJBFd8QI9MOmIZs2zsqu7SguPUm7oiTiqDdwJHygDPp6892Ueb_3I-RpZE6-Dqqi_Ezsgy7Yua-41SIBW0RIggoyh-7A3xOGx5aIREYU9BYxjuGaazoG1sBSAU3B6fWtWb6SXHM9twn8et4OqBGhOgw5GnYoLqFI64MAqOSwe4gLgD0YYJm5upj-egL0XPmtbrJgtGyNy6QgZzchlGXL0P9maRj6OOPDKfxgtxXdrbhk9b2e75uXFph1kLtvNPCHlqxJStqYl5kb7vV65d0xLP1JkVNv8BN9iOpmToH_wWVtr2XHMJKcMG5gUgLPbTT0-bNxiZObBhn3E5z8zZDYQORDU6NtF17jmwzc7GMp-zRcnli4dzHN-_wUk2eP9zrSoShHL-74wimE5GwMWT-8cX5ji9W1D4ewkdTwFKwn12lmNP9XApqs82NcCgYQY4VeyHbgt-Fe7pet6-0UG-g17CAOJREU5iSrZgq8yHhcynIYaUNGisr0MAV01QsBwLXtM46tL8JL0ALFxcDHCNlpPQX_qg4GKpFr-Gx1VhEnekIfKwDY8udPbll9G0N60-aIfeWxdWF74WAsqPhmZaJUhJnVgsZaT93FIJEWev9UotnU7z9Tr7Hq8PZID7rJcDwiYc-g");
//        recaptchaEvent.setExpectedAction("login");
//        recaptchaReqDTO.setEvent(recaptchaEvent);
//        String body = JSON.toJSONString(recaptchaReqDTO, SerializerFeature.WriteMapNullValue);
//        HttpResponse httpResponse = HttpNewUtils.doPostByApplicationJson(requestUrl, null, httpClient, null, querys, body);
//        System.out.println(httpResponse.getStatusLine());
//        String result = EntityUtils.toString(httpResponse.getEntity());
//        System.out.println(result);
//    }

    public static void main(String[] args) throws Exception {
        String apiKey = "AIzaSyASGLW2py0nsgOySX3bKmw7-ZTafmtfw7A";
        String requestUrl = "https://recaptchaenterprise.googleapis.com/v1/projects/lanzhizhuxia/assessments";
        HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();
        Map<String, String> querys = new HashMap<>();
        querys.put("key", apiKey);
        RecaptchaReqDTO recaptchaReqDTO = new RecaptchaReqDTO();
        RecaptchaEvent recaptchaEvent = new RecaptchaEvent();
        recaptchaEvent.setSiteKey("6Lee36MkAAAAAO5exxBKI2TJQ9rMpwikPtVCQZ91");
        recaptchaEvent.setToken("03AFY_a8WuYDyi8nW1K8lE8GRIppVP4cciqP_diEqcXcdcP8Z3Sq--3SvQxO1OSviusrvl9LVyhDiCCd6i1WXCRX5vEqd98EI9JmGdhphkLdhJVf0Uq8reBOonyyOhdPsGN7kn91TvcerL1OCzWdMyJ5cYRRIIO9WgSmMIqfGKntUuNLchV04p2-WmLkQJJFxawnn6wka11Pw5VDci0cSlSDeBQgQGZPFBynxyLxTaGe7cM0_JFupJu19l3p2v7ZJrCBZAl1cHVrego0uyTgCZQm5m23bL1DdcHNlpthJHu_bVS7TVN3i8azZg1N6esl8joyONXQ-VwEfCWLa5beU5tC_eaD09Gf5qrnWfW80JY50QT0BNw0KSIH-lI00qdTLNgwWrnntt9Ff7hwwd0yKmO9WLdlGeOOuFUNQxtjjb4A2OOkz3VVVsMZpG0dktMlOjDagNPUWz3VBIukYt0Vu-Qcf3Otm_R1CHzI3cgrGh7HTK4lbrR6sB80LMn7fe8MSncxCw12xtZPhd9H_-xFTKK2w5BTOJur6xXWMwPdipwhn3MXJse1HcpgsaFHUKjTZx7D4OecY3XRO3g_Y-A62JtBEH5BV5odbH5JtA86mkJ1eHUSMopBV-UJyDGaVSc8RUK8VpKPS37e0XKJaNaHtAvFKhxzuc-k767oIYDbzdhYdaxHM1_-YkKhybsBBa74xQjNi9j7CgHHbDxx_kGWkCUpny3pciJRZdG7yCcvvxQ_K23ox2uG-VdJpssZBQZoiAOefX_MiWKyUBB86HvX8Pbgm5d_hGenydl45JPGPMobzb6ThHS1afmP2q1OM_zrGABtk9H1G_hwF9h2w4fqHFn-eB6B3FeHllwcw0ox_oFDkD_k4RboCPminpRiZbSEr563C2HKDbpLkHXdP8CLnED6RGkfQWrqsIuPVJ0TpspOl1cSAfd29h1qfT1XUxPNx7HooOTMox3aoPW60Ka8l2xX4km-YRbtrJBFd8QI9MOmIZs2zsqu7SguPUm7oiTiqDdwJHygDPp6892Ueb_3I-RpZE6-Dqqi_Ezsgy7Yua-41SIBW0RIggoyh-7A3xOGx5aIREYU9BYxjuGaazoG1sBSAU3B6fWtWb6SXHM9twn8et4OqBGhOgw5GnYoLqFI64MAqOSwe4gLgD0YYJm5upj-egL0XPmtbrJgtGyNy6QgZzchlGXL0P9maRj6OOPDKfxgtxXdrbhk9b2e75uXFph1kLtvNPCHlqxJStqYl5kb7vV65d0xLP1JkVNv8BN9iOpmToH_wWVtr2XHMJKcMG5gUgLPbTT0-bNxiZObBhn3E5z8zZDYQORDU6NtF17jmwzc7GMp-zRcnli4dzHN-_wUk2eP9zrSoShHL-74wimE5GwMWT-8cX5ji9W1D4ewkdTwFKwn12lmNP9XApqs82NcCgYQY4VeyHbgt-Fe7pet6-0UG-g17CAOJREU5iSrZgq8yHhcynIYaUNGisr0MAV01QsBwLXtM46tL8JL0ALFxcDHCNlpPQX_qg4GKpFr-Gx1VhEnekIfKwDY8udPbll9G0N60-aIfeWxdWF74WAsqPhmZaJUhJnVgsZaT93FIJEWev9UotnU7z9Tr7Hq8PZID7rJcDwiYc-g");
        recaptchaEvent.setExpectedAction("login");

        String userIdentifier = "bafang" + UUID.randomUUID().toString().split("-")[0];

        // Change this to a secret not shared with Google.
        final String HMAC_KEY = "BAFANG_HMAC_KEY";

        // Get instance of Mac object implementing HmacSHA256, and initialize it with the above
        // secret key.
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
                "HmacSHA256"));
        byte[] hashBytes = mac.doFinal(userIdentifier.getBytes(StandardCharsets.UTF_8));
        ByteString hashedAccountId = ByteString.copyFrom(hashBytes);
        recaptchaEvent.setHashedAccountId(hashedAccountId);
        recaptchaReqDTO.setEvent(recaptchaEvent);
        String body = JSON.toJSONString(recaptchaReqDTO, SerializerFeature.WriteMapNullValue);
        System.out.println(body);
        HttpResponse httpResponse = HttpNewUtils.doPostByApplicationJson(requestUrl, null, httpClient, null, querys, body);
        System.out.println(httpResponse.getStatusLine());
        String result = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(result);
    }

    @ResponseBody
    @RequestMapping("test3")
    public void test3(){
        try{
            String apiKey = "AIzaSyASGLW2py0nsgOySX3bKmw7-ZTafmtfw7A";
            String requestUrl = "https://recaptchaenterprise.googleapis.com/v1/projects/lanzhizhuxia/assessments";
            HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();
            Map<String, String> querys = new HashMap<>();
            querys.put("key", apiKey);
            RecaptchaReqDTO recaptchaReqDTO = new RecaptchaReqDTO();
            RecaptchaEvent recaptchaEvent = new RecaptchaEvent();
            recaptchaEvent.setSiteKey("6Lc4dxolAAAAAGTV9-r3VR5rDtzrbBQUoSEjKzzY");
            recaptchaEvent.setToken("03AFY_a8WuYDyi8nW1K8lE8GRIppVP4cciqP_diEqcXcdcP8Z3Sq--3SvQxO1OSviusrvl9LVyhDiCCd6i1WXCRX5vEqd98EI9JmGdhphkLdhJVf0Uq8reBOonyyOhdPsGN7kn91TvcerL1OCzWdMyJ5cYRRIIO9WgSmMIqfGKntUuNLchV04p2-WmLkQJJFxawnn6wka11Pw5VDci0cSlSDeBQgQGZPFBynxyLxTaGe7cM0_JFupJu19l3p2v7ZJrCBZAl1cHVrego0uyTgCZQm5m23bL1DdcHNlpthJHu_bVS7TVN3i8azZg1N6esl8joyONXQ-VwEfCWLa5beU5tC_eaD09Gf5qrnWfW80JY50QT0BNw0KSIH-lI00qdTLNgwWrnntt9Ff7hwwd0yKmO9WLdlGeOOuFUNQxtjjb4A2OOkz3VVVsMZpG0dktMlOjDagNPUWz3VBIukYt0Vu-Qcf3Otm_R1CHzI3cgrGh7HTK4lbrR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aT93FIJEWev9UotnU7z9Tr7Hq8PZID7rJcDwiYc-g");
            recaptchaEvent.setExpectedAction("login");

            String userIdentifier = "bafang" + UUID.randomUUID().toString().split("-")[0];

            // Change this to a secret not shared with Google.
            final String HMAC_KEY = "BAFANG_HMAC_KEY";

            // Get instance of Mac object implementing HmacSHA256, and initialize it with the above
            // secret key.
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
                    "HmacSHA256"));
            byte[] hashBytes = mac.doFinal(userIdentifier.getBytes(StandardCharsets.UTF_8));
            ByteString hashedAccountId = ByteString.copyFrom(hashBytes);
            recaptchaEvent.setHashedAccountId(hashedAccountId);
            recaptchaReqDTO.setEvent(recaptchaEvent);
            String body = JSON.toJSONString(recaptchaReqDTO);
            HttpResponse httpResponse = HttpNewUtils.doPostByApplicationJson(requestUrl, null, httpClient, null, querys, body);
            System.out.println(httpResponse.getStatusLine());
            String result = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(result);
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<>(body, httpHeaders);
            Object o = restTemplate.postForObject(requestUrl, entity, Object.class);
            System.out.println(JSON.toJSONString(o));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}